<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 下午 01:21
 */

namespace app\admin\controller;


use think\Exception;
use think\Request;

class Activity extends Common
{
    /**
     * @return \think\response\View
     * 活动列表
     */
    public function index()
    {
        $activity_name = '';
        $data = [];
        if(Request::instance()->isPost()){
            $activity_name = input("post.activity_name");
            $data['activity_name'] = $activity_name;
        }
        $activity = new \app\admin\model\Activity();
        $list = $activity->activity_list($activity_name);
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        $num_per_page = config('num_per_page');
        $data['num_per_page'] = $num_per_page;
        $count = $activity->activity_count($activity_name);
        $data['list'] = $list;
        $data['count'] = $count;
        return view('',$data);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @throws \Exception
     * 添加
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            $info = input("post.");
            $arr = [];
            foreach($info['gift_id'] as $value){
                array_push($arr,$value);
                $info['gift_id'] = implode(',',$arr);
            }
            if ($info['activity_name'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '活动名称不能为空',
                ]);
            }
            $activity_name = db('activity')
                ->field('id')
                ->where('activity_name',$info['activity_name'])
                ->find();
            if ($activity_name) {
                return json([
                    'statusCode' => 300,
                    'message' => '活动名称不能与已有活动重复',
                ]);
            }
            if ($info['gift_id'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '活动礼物不能为空',
                ]);
            }
//            if ($info['activity_img'] == '') {
//                return json([
//                    'statusCode' => 300,
//                    'message' => '请上传图片',
//                ]);
//            }
            if ($info['opentime'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '开始时间不能为空',
                ]);
            }
            if ($info['closetime'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '结束时间不能为空',
                ]);
            }
//            if ($info['sort'] == '') {
//                return json([
//                    'statusCode' => 300,
//                    'message' => '排序不能为空',
//                ]);
//            }
            try {
                $activity = new \app\admin\model\Activity();
                $activity->doAdd($info);
            } catch (Exception $e) {
                return json([
                    'statusCode' => 300,
                    'message' => $e->getMessage(),
                ]);
            }
            return json([
                'statusCode' => 200,
                'message' => '添加成功',
                'navTabId' => 'activityManage',
                'callbackType' => 'closeCurrent',
            ]);
        }
        $gift = db('gift')
                ->field("id,gift_name")
                ->where("is_delete",0)
                ->select();
        $data['gift'] = $gift;
        return view("",$data);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * 修改
     */
    public function edit()
    {
        $id = input('get.id');
        if(Request::instance()->isPost()){
            $info = input("post.");
            $arr = [];
            foreach($info['gift'] as $key=>$value){
                array_push($arr,$value);
                $info['gift_id'] = implode(',',$arr);
                unset($info['gift']);
            }
            if ($info['activity_name'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '活动名称不能为空',
                ]);
            }
            $activity_name = db('activity')
                ->field('id')
                ->where('activity_name',$info['activity_name'])
                ->where('id','neq',$info['activity_id'])
                ->find();
            if ($activity_name) {
                return json([
                    'statusCode' => 300,
                    'message' => '活动名称不能与已有活动重复',
                ]);
            }
            if ($info['gift_id'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '活动礼物不能为空',
                ]);
            }
//            if ($info['activity_img'] == '') {
//                return json([
//                    'statusCode' => 300,
//                    'message' => '请上传图片',
//                ]);
//            }
            if ($info['opentime'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '开始时间不能为空',
                ]);
            }
            if ($info['closetime'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '结束时间不能为空',
                ]);
            }
//            if ($info['sort'] == '') {
//                return json([
//                    'statusCode' => 300,
//                    'message' => '排序不能为空',
//                ]);
//            }
            try {
                $activity = new \app\admin\model\Activity();
                $activity->doEdit($info);
            } catch (Exception $e) {
                return json([
                    'statusCode' => 300,
                    'message' => $e->getMessage(),
                ]);
            }
            return json([
                'statusCode' => 200,
                'message' => '修改成功',
                'navTabId' => 'activityManage',
                'callbackType' => 'closeCurrent',
            ]);
        }
        $activity = db("activity")
                    ->where("id",$id)
                    ->where("is_delete",0)
                    ->find();
        $gift = db('gift')
            ->field("id,gift_name")
            ->where("is_delete",0)
            ->select();
        $data['activity'] = $activity;
        $data['activity']['gift_arr'] = explode(",",$activity['gift_id']);
        $data['gift'] = $gift;
        $data['id'] = $id;
        return view("modify",$data);
    }

    /**
     * @return \think\response\Json
     * 删除
     */
    public function del()
    {
        $id = input('get.id');
        try {
            $new_del = new \app\admin\model\Activity();
            $new_del->doDelete($id);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => 'activityManage',
        ]);
    }

    public function detail()
    {
        $id = input("get.id");
        $activity = db("activity")->where("id",$id)->where("is_delete",0)->find();
        $arr = explode(",",$activity['gift_id']);
        $gift = db("gift")->field("gift_name")->where("is_delete",0)->whereIn("id",$arr)->select();
        $data['gift'] = $gift;
        $data['activity'] = $activity;
        return view('',$data);
    }
    /**
     * 启用-禁用
     */
    public function on_off()
    {
        $userId = input('get.id');
        $statusid = input('get.status');
        if($statusid == 0){
            $data['id'] = $userId;
            $data['is_use'] = 0;
            $status = "禁用";
        }
        if($statusid == 1){
            $data['id'] = $userId;
            $data['is_use'] = 1;
            $status = "启用";
        }
        try {
            $activity = new \app\admin\model\Activity();
            $activity->editStatus($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => $status.'成功',
            'navTabId' => 'activityManage',
        ]);
    }
}